﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Messaging;
using System.ServiceModel;
using System.Text;
using WCFIServices;
using WCFServerService;

namespace ServerApplication
{
    class Program
    {
        static ServiceHost _registerClientServiceHost = null;
        
        static void Main(string[] args)
        {
            if (_registerClientServiceHost == null)
            {
                string registerClientMSMQName = ConfigurationManager.AppSettings["RegisterClientMSMQName"];

                if (!MessageQueue.Exists(registerClientMSMQName))
                {
                    MessageQueue versionManagerQueue = MessageQueue.Create(registerClientMSMQName, true);
                    versionManagerQueue.SetPermissions(@"Everyone", MessageQueueAccessRights.FullControl);

                    versionManagerQueue.SetPermissions(@"ANONYMOUS LOGON",
                        MessageQueueAccessRights.ReceiveMessage |
                        MessageQueueAccessRights.PeekMessage |
                        MessageQueueAccessRights.WriteMessage);
                }

                _registerClientServiceHost = new ServiceHost(typeof(RegisterClientService));
                _registerClientServiceHost.Open();

               Console.WriteLine("*** Server Started Successfully ***");

                RecursiveReadLine();
            }
        }

        public static void RecursiveReadLine()
        {
            Console.WriteLine("=======================================================");
            Console.WriteLine("Do you wanna send data from Server to Client 'Y' or 'N' ?");
            var result = Console.ReadLine();
            if (result != null && result.ToLower() == "y")
            {
                Console.WriteLine("");
                ObjectCreation();
                //WcfServerPushServerBO.Instance.Message = readMsg;
                RecursiveReadLine();
            }
            else if (result != null && result.ToLower() == "n")
            {

            }
            else
            {
                Console.WriteLine("*** Wrong Option : Try Again ***");
                RecursiveReadLine();
            }
        }

        private static void ObjectCreation()
        {
            Console.WriteLine("Select the option :::");
            Console.WriteLine("'S' to Sample object \n'N' to Create New object");
            var readMsg = Console.ReadLine();
            if (readMsg != null && readMsg.ToLower() == "s")
            {
                WcfServerPushServerBO.Instance.Employee = new Employee() { Name = "Amarnath", Position = "CEO", YearsOfExp = 12 };

                Console.WriteLine("\n\n\n *** Data Pushed to Client Sucessfully *** \n\n");
            }
            else if (readMsg != null && readMsg.ToLower() == "n")
            {
                var employee = new Employee();

                Console.WriteLine("\nEnter Employee Details");
                Console.WriteLine("Name :");
                employee.Name = Console.ReadLine();

                Console.WriteLine("Position :");
                employee.Position = Console.ReadLine();

                Console.WriteLine("YearsOfExp :");
                employee.YearsOfExp = Convert.ToDouble(Console.ReadLine());

                WcfServerPushServerBO.Instance.Employee = employee;
                Console.WriteLine("\n\n\n *** Data Pushed to Client Sucessfully *** \n\n");
            }
            else
            {
                ObjectCreation();
            }
        }
    }
}
